﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class application_step1 :  DAO.ESWebUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Int64 langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            Int64.TryParse(Session["language"].ToString(), out langId);
        InitializeUI();
        LoadAppTypes(langId);
        LoadTransportTypes(langId);
        setAttributes();
        if (Request.Cookies["texchange"] != null && Request.Cookies["texchange"]["cid"]!=null)
			tb_comany_id.Value = DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["cid"].ToString());

    }

    private void setAttributes()
    {
        ddlTransport.Attributes.Add("validate", "true");
        ddlOffer.Attributes.Add("validate", "true");        
    }

    private void LoadAppTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc')",conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 12);

                foreach (SqlParameter s in pars)
                {
                    cmd.Parameters.Add(s);
                }
                SqlDataReader dr = cmd.ExecuteReader();
                ddlOffer.Items.Clear();
                ddlOffer.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (dr["isActive"].ToString() == "True")
                    {
                        if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                        {
                            ddlOffer.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                        }
                        else
                        {
                            ddlOffer.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                        }
                    }
                }
                dr.Close();
            }
         }
           
    }

    private void LoadTransportTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc')", conn))
            {

                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 9);

                foreach (SqlParameter s in pars)
                {
                    cmd.Parameters.Add(s);
                }
                SqlDataReader dr = cmd.ExecuteReader();
                ddlTransport.Items.Clear();
                ddlTransport.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (dr["isActive"].ToString() == "True")
                    {
                        if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                        {
                            ddlTransport.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                        }
                        else
                        {
                            ddlTransport.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                        }
                    }
                }
                dr.Close();
            }
        }
    }

    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);
        this.LangId = Convert.ToInt32(langId);
    }


}
